package sdufe.lele.service;


import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * Created by geniee on 17-4-25.
 */

@Repository
public class RedisDateTimeService {

    @Resource
    private RedisTemplate<String, LocalDateTime> redisTemplate;

    /**
     * @param key
     * @return
     */
    public LocalDateTime searchByKey(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * @param key
     * @param value
     */
    public void setByKey(String key, LocalDateTime value) {
        redisTemplate.opsForValue().set(key, value);
    }

    @Transactional
    public void updateByKey(String key, LocalDateTime value) {
        redisTemplate.delete(key);
        redisTemplate.opsForValue().set(key, value);
    }

    public void deleteByKey(String key) {
        if (key!=null &&isExistKey(key)) {
            redisTemplate.delete(key);
        }
    }

    public Boolean isExistKey(String key) {

            return redisTemplate.hasKey(key);

    }
}
